package org.xenon.vote.service;

import org.springframework.stereotype.Service;
import org.xenon.vote.dao.imp.JudgeDao;
import org.xenon.vote.dao.imp.UserDao;
import org.xenon.vote.model.pojo.po.Judge;
import org.xenon.vote.model.pojo.po.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class JudgeService {
    private final JudgeDao judgeDao;
    private final UserDao userDao;

    public JudgeService(JudgeDao judgeDao, UserDao userDao) {
        this.judgeDao = judgeDao;
        this.userDao = userDao;
    }

    public Judge login(String name, String owner) {
        return judgeDao.get(name, owner);
    }

    /**
     * 以备不时之需
     */
    public JudgeDao getJudgeDao() {
        return judgeDao;
    }

    /**
     * 代理
     */
    public Judge get(String name, String owner) {
        return judgeDao.get(name, owner);
    }

    /**
     * 获取某场次的所有裁判
     * @param id 轮次ID
     */
    public List<Judge> get(int id) {
        List<Judge> list = judgeDao.getAll(id);
        if (list.size() == 0) return null;
        return list;
    }

    /**
     * 验证该人是否存在
     */
    public boolean testJudge(String name) {
        return judgeDao.get(name) != null;
    }

    /**
     * 修改一些裁判的密码
     * @param judges 裁判<名字, 新密码>
     * @return 失败名单
     */
    public List<String> edit(HashMap<String, String> judges, String mail) {
        List<String> err = new ArrayList<>();
        for (String s : judges.keySet())
            if (!edit(s, judges.get(s), mail)) err.add(s);
        return err;
    }

    /**
     * 修改一个裁判的密码
     * @param name 裁判的名字
     * @param passwd 新的密码
     * @return 是否成功 如果未找到则返回false
     */
    public boolean edit(String name, String passwd, String mail){
        Judge judge = judgeDao.get(name);
        if (judge == null) return false;
        if (!judge.getOwner().equals(mail)) return false;
        judge.setPassword(passwd);
        judgeDao.edit(judge);
        return true;
    }

    /**
     * 删除一个裁判
     *
     * @param name 目标的名字
     * @return 是否成功
     */
    public boolean del(String name, String owner) {
        Judge judge = judgeDao.get(name);
        if (judge == null) return false;
        if (!judge.getOwner().equals(owner)) return false;
        judgeDao.del(name);
        return true;
    }

    /**
     * 删除一些裁判
     *
     * @param list 删除名单的名字
     * @return 失败名单
     */
    public List<String> del(String[] list, String owner) {
        List<String> err = new ArrayList<>();
        for (String i : list)
            if (!del(i, owner)) err.add(i);
        return err;
    }

    /**
     * 添加一个裁判
     *
     * @param name   裁判名字
     * @param passwd 裁判密码
     * @param turn   所属轮次
     * @return 是否重名, {@code true}则成功
     */
    public boolean add(String name, String passwd, int turn, String owner) {
        try {
            judgeDao.add(new Judge(name, passwd, turn, owner));
        }catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 不需要事先测试judge是否存在
     *
     * @param judges 裁判账密 <账号, 密码>
     * @param turn   所属的投票编号
     * @return 注册失败的裁判编号
     */
    public List<String> add(HashMap<String, String> judges, int turn, String owner) {
        ArrayList<String> err = new ArrayList<>();
        for (String s : judges.keySet())
            if (!add(s, judges.get(s), turn, owner)) err.add(s+" "+judges.get(s));
        return err;
    }

    /**
     * 清除一个轮次的裁判
     */
    public void clear(int turn){
        judgeDao.delAll(turn);
    }
}
